package com.liu.nb.util.exception;

import com.liu.nb.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
@name:
@description:全局异常处理
@author: lzy
@title: 天王盖地虎!!!
@time:  2021-08-07 12:38:15
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) {
        log.error("Assert异常：----------------{}", e.getMessage());
        return Result.fail(e.getMessage());
    }
    //如果是权限
    @ExceptionHandler(value = AccessDeniedException.class)
    public void handler(AccessDeniedException exception){
        throw exception;
    }
    //    返回响应状态
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = Exception.class)
    public Result handler(RuntimeException exception){
        return Result.fail(exception.getMessage());
    }
}
